package org.fly.sys.controller;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author:whh
 * @date: 2025-11-29 08:57
 * <p></p>
 */
@RestController
@RequestMapping("file")
public class FileController {


    @Autowired
    @Qualifier("serialExecutor")
    private ThreadPoolExecutor serialExecutor;


    @GetMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {




            response.setCharacterEncoding("UTF-8");
            Future<Void> submit = serialExecutor.submit(() -> {


                File file = new File("/dbeaver.zip");

                byte[] data = new byte[(int) file.length()];
                ByteArrayInputStream in = null;
                try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {

                    int read = bis.read(data);

                    Thread.sleep(1000);

                    in = new ByteArrayInputStream(data);


                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                ServletUtil.write(response, in);

                return null;
            });


            submit.get();
        }catch (RejectedExecutionException rjx){

            rjx.printStackTrace();



            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"系统繁忙");

        }catch (Exception ex){

            ex.printStackTrace();
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,"系统繁忙++++++++++++++");
        }






    }














    @GetMapping("download2")
    public void download2(HttpServletRequest request, HttpServletResponse response) throws IOException {



            response.setCharacterEncoding("UTF-8");
            File file = new File("/dbeaver.zip");

            byte[] data = new byte[(int) file.length()];
            ByteArrayInputStream in = null;
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {

                int read = bis.read(data);
                in = new ByteArrayInputStream(data);
                ServletUtil.write(response, in);

            } catch (IOException rjx) {

                rjx.printStackTrace();


                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "系统繁忙");

            } catch (Exception ex) {

                ex.printStackTrace();
                response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "系统繁忙++++++++++++++");
            }



    }





}
